package com.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.ProjectOutside;
import com.group.entity.Talents;
import com.group.mapper.ProjectOutsideMapper;
import com.group.service.ProjectOutsideService;
import com.group.service.impl.ProjectOutsideServiceImpl;
import com.group.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
//@RequestMapping("/project-outside")
public class ProjectOutsideController {

    @Autowired
    ProjectOutsideMapper projectOutsideMapper;

    @Autowired
    ProjectOutsideService projectOutsideService;

    @Autowired
    private ProjectOutsideServiceImpl pos;

    private Page<ProjectOutside> getPages(Integer currentPage, Integer pageSize, List list) {
        Page page = new Page();
        int size = list.size();

        if (pageSize > size) {
            pageSize = size;
        }

        // 求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

        if (currentPage > maxPage) {
            currentPage = maxPage;
        }

        // 当前页第一条数据的下标
        int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;

        List pageList = new ArrayList();

        // 将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIdx + i < size; i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }


    @GetMapping("/projectlist")
    public String projectOutside(Model model, HttpSession session) {
        List<ProjectOutside> projectOutsideList = (List<ProjectOutside>) session.getAttribute("projectsList");
        Long allPages2 = (Long) session.getAttribute("allPages2");
        model.addAttribute("projectOutsideList", projectOutsideList);
        model.addAttribute("allPages2", allPages2);
        return "lookingprojects";
    }

    //通过项目Id查找人才
    @GetMapping("/queryProjectById")
    public String queryTalentById(@RequestParam Integer projectId, Model model) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("projectId", projectId);
        List<ProjectOutside> queryProjectByIdList = projectOutsideMapper.selectList(wrapper);
        model.addAttribute("projectOutsideList", queryProjectByIdList);
        return "lookingprojects";
    }


    @GetMapping("/getProjectListByTag")
    @ResponseBody
    public DataVO getProjectListByTag(Integer page, Integer limit, @RequestParam String zone) {
        return projectOutsideService.findData2(page, limit, zone);
    }

    @GetMapping("/lookDetailedProject/{projectId}")
    public String lookDetailedProject(@PathVariable("projectId") Integer projectId, Model model) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("projectId", projectId);
        ProjectOutside projectOutside = projectOutsideService.getOne(wrapper);
        model.addAttribute("projectOutside", projectOutside);
        return "clickprojectpage";
    }

}

